package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.admin.vo.CompanyBillDailyStatSumVo;
import com.net.xpay.admin.vo.CompanyBillDailyStatVo;
import com.net.xpay.common.domain.stat.CompanyBillDailyStat;
import com.net.xpay.common.manager.partner.PartnerInsiderSettleLogManager;
import com.net.xpay.common.manager.stat.CompanyBillDailyStatManager;
import com.net.xpay.common.model.search.CompanyBillDailyStatSearchModel;
import com.net.xpay.core.model.PartnerInsiderSettleLogSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

/**
 *

 * 公司账单日常统计
 */
@RestController
@RequestMapping("/admin/company-bill-daily-stat")
@Api(tags = "运营/公司账单日常统计", description = "运营 公司账单日常统计 net")
public class AdminCompanyBillDailyStatController {

    @Autowired
    private CompanyBillDailyStatManager companyBillDailyStatManager;

    @Autowired
    private PartnerInsiderSettleLogManager partnerInsiderSettleLogManager;

//    @ApiOperation("搜索每日统计")
//    @PostMapping("/search")
//    @ResponseBody
//    @PreAuthorize("hasAnyRole('ADMIN', 'company-bill-daily-stat::search')")
//    public ResultPagedList<CompanyBillDailyStat> searchCompanyBillDailyStat(@RequestBody CompanyBillDailyStatSearchModel searchModel) {
//        List<CompanyBillDailyStat> records = companyBillDailyStatManager.search(searchModel);
//        long count = companyBillDailyStatManager.searchCount(searchModel);
//        return new ResultPagedList<>(records, count, searchModel);
//    }

    //todo 数据处理 大部分数据都除了100
    @ApiOperation("搜索每日统计")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'company-bill-daily-stat::search')")
    public ResultPagedList<CompanyBillDailyStatVo> searchCompanyBillDailyStat(@RequestBody CompanyBillDailyStatSearchModel searchModel) {
        if (searchModel.getYear() != null && searchModel.getMonth() != null) {
            LocalDate startDay = LocalDate.of(searchModel.getYear(), searchModel.getMonth(), 1);
            searchModel.setStartDay(startDay);
            searchModel.setEndDay(LocalDateUtil.getMonthEndDay(startDay));
        }
        List<CompanyBillDailyStat> records = companyBillDailyStatManager.search(searchModel);
        List<CompanyBillDailyStatVo> vos = BeanCopierUtil.batchCopy(CompanyBillDailyStat.class, CompanyBillDailyStatVo.class, records);
        for (CompanyBillDailyStatVo stat : vos) {
            //预估充值手续费
            BigDecimal estimateChargeCost = stat.getEstimateChargeCost();
            BigDecimal estimateBankCost = stat.getEstimateBankCost();
            BigDecimal estimateTotalCost = estimateBankCost.add(estimateChargeCost);

            BigDecimal totalSuccessAmount = stat.getSuccessAmount().add(stat.getSafePaySuccessAmount());
            //合计平台收益
            BigDecimal totalBenefit = (new BigDecimal(stat.getTotalPlatformBenefit()).add(new BigDecimal(stat.getSafePayTotalPlatformBenefit()))).multiply(new BigDecimal("0.01")).subtract(estimateTotalCost);
            //合计服务费收入
            BigDecimal totalServicePrice = new BigDecimal(stat.getSuccessServicePrice()).add(new BigDecimal(stat.getSafePaySuccessServicePrice())).multiply(new BigDecimal("0.01"));
            if (totalSuccessAmount.compareTo(BigDecimal.ZERO) != 0){
                stat.setTotalPlatformBenefitRatio(totalBenefit.divide(totalSuccessAmount,4,RoundingMode.HALF_UP));
                stat.setTotalAverageRatio(totalServicePrice.divide(totalSuccessAmount,4,RoundingMode.HALF_UP));
            }

            stat.setEstimateChargeCost(estimateChargeCost.multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
            stat.setEstimateBankCost(estimateBankCost.multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
            stat.setEstimateTotalCost(estimateTotalCost.multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));


            stat.setSuccessServicePrice(stat.getSuccessServicePrice() / 100);
            stat.setSafePaySuccessServicePrice(stat.getSafePaySuccessServicePrice() / 100);

            stat.setTotalPlatformBenefit(stat.getTotalPlatformBenefit() / 100);
            stat.setSafePayTotalPlatformBenefit(stat.getSafePayTotalPlatformBenefit() / 100);

            stat.setTotalPartnerBenefit(stat.getTotalPartnerBenefit() / 100);
            stat.setTotalFatherPartnerBenefit(stat.getTotalFatherPartnerBenefit() / 100);
            stat.setSafePayTotalPartnerBenefit(stat.getSafePayTotalPartnerBenefit() / 100);
            stat.setSafePayTotalFatherPartnerBenefit(stat.getSafePayTotalFatherPartnerBenefit() / 100);
            stat.setRechargeAmount(stat.getRechargeAmount() / 100);
            stat.setReverseRechargeAmount(stat.getReverseRechargeAmount() / 100);



        }
        long count = companyBillDailyStatManager.searchCount(searchModel);
        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("按日期范围合计收益")
    @PostMapping("/sum")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'company-bill-daily-stat::search')")
    public ResultData<CompanyBillDailyStatSumVo> sumCompanyBillDailyStat(@RequestBody CompanyBillDailyStatSearchModel searchModel) {
        if (searchModel.getYear() != null && searchModel.getMonth() != null) {
            LocalDate startDay = LocalDate.of(searchModel.getYear(), searchModel.getMonth(), 1);
            searchModel.setStartDay(startDay);
            searchModel.setEndDay(LocalDateUtil.getMonthEndDay(startDay));
        }
        CompanyBillDailyStat companyBillDailyStat = companyBillDailyStatManager.sumCompanyBillDailyStat(searchModel);
        CompanyBillDailyStatSumVo vo = new CompanyBillDailyStatSumVo();
        BeanCopierUtil.copy(companyBillDailyStat, vo);
        //预估充值手续费
        BigDecimal estimateChargeCost = companyBillDailyStat.getEstimateChargeCost();
        BigDecimal estimateBankCost = companyBillDailyStat.getEstimateBankCost();
        BigDecimal estimateTotalCost = estimateBankCost.add(estimateChargeCost);

        vo.setEstimateChargeCost(estimateChargeCost);
        vo.setEstimateBankCost(estimateBankCost);
        vo.setEstimateTotalCost(estimateTotalCost);
        vo.setPartnerInsiderBenefit(new BigDecimal("0.00"));

        if (searchModel.getYear() != null && searchModel.getMonth() != null) {
            //查询内部员工的月收益
            PartnerInsiderSettleLogSearchModel model = new PartnerInsiderSettleLogSearchModel();
            model.setYear(searchModel.getYear());
            model.setMonth(searchModel.getMonth());
            BigDecimal partnerInsiderBenefit = partnerInsiderSettleLogManager.sumPartnerSettleAmount(model);
            vo.setPartnerInsiderBenefit(partnerInsiderBenefit);
        }

        if (searchModel.getYear() == null && searchModel.getMonth() == null && searchModel.getStartDay() == null && searchModel.getEndDay() == null) {
            PartnerInsiderSettleLogSearchModel model = new PartnerInsiderSettleLogSearchModel();
            BigDecimal partnerInsiderBenefit = partnerInsiderSettleLogManager.sumPartnerSettleAmount(model);
            vo.setPartnerInsiderBenefit(partnerInsiderBenefit);
        }

        return new ResultData<>(vo);
    }
}
